import QtQuick 1.0
import "../Util/Util.js" as Util

ModelXmlList {
//    id: _root

    property bool isFirst: true         //最初に1回だけ処理する内容の為のフラグ

    property bool accessReady: true     //上位で読込みに関するReady状態を指定する
    property bool isReady: ready && accessReady    //状態の合成（最終的にこれを見る）

    property bool ready: status == XmlListModel.Ready

    property int sourceType: 0              //取得するデータの種類    0:timeline 1:mention

    property string listId: ""              //リストのTLを表示する時のID
    property string listIdPrev: ""          //前回表示してたlistのID

    property string queryParam1: "user"     //ユーザー情報系のディレクトリ（通常TLとDMを切り替える）

    property variant objTwitter: null       // TwitterAPIアクセス用エレメント
    property int updateCount: 30            // 取得数

    property int favPageNum: 1

    //xmlの解析関係
    query: makeQuery();//"/statuses/status"
    XmlRole { name: "_status_id"; query: "id/string()"; isKey: true}
    XmlRole { name: "_name"; query: queryParam1 + "/name/string()"; }
    XmlRole { name: "_screen_name"; query: queryParam1 + "/screen_name/string()"; }
    XmlRole { name: "_text"; query: "text/string()"; }
    XmlRole { name: "_icon"; query: queryParam1 + "/profile_image_url/string()";}
    XmlRole { name: "_protected"; query: queryParam1 + "/protected/string()";}
    XmlRole { name: "_date"; query: "created_at/string()"; }
    XmlRole { name: "_in_reply_to_status_id"; query: "in_reply_to_status_id/string()"; }
    XmlRole { name: "_in_reply_to_screen_name"; query: "in_reply_to_screen_name/string()"; }
    XmlRole { name: "_source"; query: "source/string()"; }
    XmlRole { name: "_favorited"; query: "favorited/string()"; }

    XmlRole { name: "_rts_name"; query: "retweeted_status/user/name/string()"; }
    XmlRole { name: "_rts_screen_name"; query: "retweeted_status/user/screen_name/string()"; }
    XmlRole { name: "_rts_text"; query: "retweeted_status/text/string()"; }
    XmlRole { name: "_rts_icon"; query: "retweeted_status/user/profile_image_url/string()";}
    XmlRole { name: "_rts_source"; query: "retweeted_status/source/string()"; }

    XmlRole { name: "_urls"; query: "entities/urls/string()"; }
    XmlRole { name: "_hashtags"; query: "entities/hashtags/string()"; }

    function clearData(){
        //カラデータを指定して無理やり消す
        //xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><statuses type=\"array\"><status><created_at /><id /><text /><source /><truncated /><favorited /><in_reply_to_status_id /><in_reply_to_user_id /><in_reply_to_screen_name /><retweet_count /><retweeted /><user><id /><name /><screen_name /><location /><description /><profile_image_url /><profile_image_url_https /><url /><protected /><followers_count /><profile_background_color /><profile_text_color /><profile_link_color /><profile_sidebar_fill_color /><profile_sidebar_border_color /><friends_count /><created_at /><favourites_count /><utc_offset /><time_zone /><profile_background_image_url /><profile_background_image_url_https /><profile_background_tile /><profile_use_background_image /><notifications /><geo_enabled /><verified /><following /><statuses_count /><lang /><contributors_enabled /><follow_request_sent /><listed_count /><show_all_inline_media /><default_profile /><default_profile_image /><is_translator /></user><geo /><coordinates /><place /><contributors /></status></statuses>";
        clear();
    }

    function makeQuery(){
        var ret = "/statuses/status";
        switch(sourceType){
        case 2:
            //DM
            ret = "/direct-messages/direct_message";
            queryParam1 = "sender";
            break;
        default:
            //timeline
            //mention
            //Lists
            //List
            //search list
            //search timeline
            //retweet by others(to me)
            //retweet by you(by me)
            //your tweets retweeted(of me)
            //favorites
            break;
        }

        return ret;
    }

    //最新のページを取得する
    function recent() {
        isAppendTop = true;
        requestContent(null);
    }

    //次のページを取得する
    function more() {
        if (isReady) {
            var max_id = null;
            if(count > 0){
                var i = get(count-1);
                max_id = i._status_id;
            }
            favPageNum++;   //fav対策
            requestContent(max_id);
        }else{
        }
    }

    // 応答結果を指定する
    function response(text){
        xml = text;
        accessReady = true;
    }



    //TwitterのURLを取得する
    function requestContent(max_id){
        var count = updateCount;
        switch(sourceType){
        case 0:
            //timeline
            accessReady = false;
            objTwitter.timelines.homeTimeline(count, null, max_id, null, null
                                  , true, true, null);
            break;
        case 1:
            //mention
            accessReady = false;
            objTwitter.timelines.mentions(count, null, max_id, null, null
                                  , true, null);
            break;
        case 2:
            //DM
            accessReady = false;
            objTwitter.directmessages.directMessages(null, max_id, count, null
                                                   , true, null);
            break;
        case 3:
            //Lists
            break;
        case 4:
            //List
            if(listId.length === 0){
            }else{
                if(listId !== listIdPrev){
                    clear();
                    listIdPrev = listId;
                }
                accessReady = false;
                objTwitter.lists.listsStatuses(listId, null, null, null
                                             , null, max_id, count, null
                                             , true, true);
            }
            break;
        case 5:
            //search list
            break;
        case 6:
            //search timeline
            break;
        case 7:
            //retweet by others(to me)
            accessReady = false;
            objTwitter.timelines.retweetedToMe(count, null, max_id, null, null
                                             , true);
            break;
        case 8:
            //retweet by you(by me)
            accessReady = false;
            objTwitter.timelines.retweetedByMe(count, null, max_id, null, null
                                             , true);
            break;
        case 9:
            //your tweets retweeted(of me)
            accessReady = false;
            objTwitter.timelines.retweetedOfMe(count, null, max_id, null, null
                                             , true);
            break;
        case 10:
            //favorites
            accessReady = false;
            objTwitter.favorites.favorites(null, null, favPageNum, true);
            break;
        case 11:
            //User Timeline
//            Util.outputDebugString("user id=" + listId + ", last id=" + listIdPrev);
            var id = listId;
            if(id.length === 0){
                // listIdに外で選択アイテムにバインドしてるので
                id = listIdPrev;
            }
            if(id.length === 0){
            }else{
                if(id !== listIdPrev){
                    clearData();
                    listIdPrev = id;
                }
                accessReady = false;
                objTwitter.timelines.userTimeline(null, id
                                                , count, null, max_id, null, null
                                                , true, true, false
                                                , null);
            }
            break;
        default:
            break;
        }
    }


}
